﻿using NB.MGJ.Common;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.Config
{
    public class ConfigManagerService
    {
        public BaseRepository _baseRepository = new BaseRepository();

        #region 微信信息配置

        /// <summary>
        /// 微信添加绑定信息
        /// </summary>
        /// <param name="model">微信信息model</param>
        /// <returns></returns>
        public int WXBindAdd(config_WX model)
        {
            return _baseRepository.Insert<config_WX>(model);
        }

        /// <summary>
        /// 查询公司是否有绑定微信信息
        /// </summary>
        /// <returns></returns>
        public int getWXBind_id(int Eid)
        {
            string sql = "select count(*) from config_WX where Eid=" + Eid;
            int i = Convert.ToInt32(_baseRepository.GetSingle(sql));
            return i > 0 ?
                 Convert.ToInt32(_baseRepository.GetSingle("select id from config_WX where Eid=" + Eid)) //ButtonId
                         : 0;
        }

        /// <summary>
        /// 编辑微信绑定数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdWXBing(config_WX model)
        {
            return _baseRepository.Update<config_WX>(model);
        }

        /// <summary>
        /// 绑定微信数据初始化加载
        /// </summary>
        /// <param name="Eid"></param>
        /// <returns></returns>
        public config_WX GetwxBindModel(int Eid)
        {
            string sql = " select * from config_WX where  Eid=" + Eid;
            return _baseRepository.ExecuteText<config_WX>(sql);
        }

        /// <summary>
        /// 微信绑定的数据 检查是否为空
        /// </summary>
        /// <param name="eid">机构编号</param>
        /// <returns>1:0</returns>
        public int CheckWXServices(int eid)
        {
            string sql = string.Format(@"SELECT
                                      	COUNT(*) as `count`
                                      FROM
                                      	config_WX
                                      WHERE
                                      	eid = {0}
                                      AND AppId <> ''
                                      AND AppSecret <> ''
                                      AND CommercialName <> ''
                                      AND mchid <> ''
                                      AND CommercialKey <> ''
                                      AND CredentialAddress<>''
                                      AND CredentialPwd<>'' ", eid);
            return Convert.ToInt32(_baseRepository.GetSingle(sql));
        }



        #endregion

        #region 短信配置
        public config_SMS GetSMSApiModel(int eId)
        {
            string sql = " select * from config_SMS where viewDel=0 and  EnterpriseId='" + eId+"'";
            return _baseRepository.ExecuteText<config_SMS>(sql);
        }
        public int UpdateSMSApi(config_SMS model)
        {
            return _baseRepository.Update<config_SMS>(model);
        }
        public int AddSMSApi(config_SMS model)
        {
            return _baseRepository.InsertReturnID<config_SMS>(model);
        }

        /// <summary>
        /// 微信绑定的数据 检查是否为空
        /// </summary>
        /// <param name="eid">机构编号</param>
        /// <returns>1:0</returns>
        public int CheckSMSServices(int eid)
        {
            string sql = string.Format(@"SELECT
                                         	COUNT(*) AS `count`
                                         FROM
                                         	config_sms 
                                         where EnterpriseId={0}
                                         and appkey <> ''
                                         and ModelCode <> '' ", eid);
            return Convert.ToInt32(_baseRepository.GetSingle(sql));
        }

        #endregion
    }
}
